package com.lmx.web.controller.thirdPart;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.DeviceService;
import com.lmx.persistence.service.EmployeeService;
import com.lmx.persistence.service.LocalHbeaconService;
import com.lmx.persistence.service.NursingService;
import com.lmx.persistence.service.TestService;
import com.lmx.persistence.service.TimerService;
import com.lmx.persistence.service.WeiXinService;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.CustomerServerChange;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/*-
 * 测试处理接口
 */

@Controller
@RequestMapping("/access/test")
public class TestController {
	private static Log logger = LogFactory.getLog(TestController.class);

	@Autowired
	private TimerService sService;
	
	@Autowired
	private TestService testService;
	
	@Autowired
	private LocalHbeaconService lService;
	
	@Autowired
	private WeiXinService wService;
	
	@Autowired
	private NursingService nService;
	
	@Autowired
	private  DeviceService dService;
	
	@Autowired
	private EmployeeService employeeService;

	@Autowired
	private NursingService nursingService;
	
	@Autowired
	private CustomerService cService;
	
	@RequestMapping("/stat")
	@ResponseBody
	public void bedMatWarningData(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		String result = "";
		logger.debug("-----------------------------start test stat-------------------------------------");
//		sService.statInTryBackQuery();
//		sService.statConsultQuery();
//		sService.statOldManQuery();
//		s2Service.getTimeServiceItem(79);
//		dService.getDeviceFromAll("101", (short)1, "deviceType3", "huamishouhuan");
//		cService.getCustomerLocationData(Long.parseLong("1014"), Long.parseLong("1"));	
//		dService.updateDeviceStatus();	
//		Customer customer=new Customer();
//		customer.setId(280L);;
//		customer=cService.getCustomer(customer);
//		cService.operateCustomerBack(null, customer);		
//      lService.addDeviceInfo("");	
//      wService.sign("http://web.tnb99.cn");
//      cService.getCustomerByEID(12L);
//      nService.getNursingServiceByCustomer(null,20l,20l,24l,"postType5");
//      sService.createCustomerMonthBill();
//      nService.getDrugProjectByCustomer(129L);
//      dService.getCustomerWithDeviceNo("11", "12");
//		sService.copyCusomerPlanDetail();
//		sService.createCustomerDayBill();
//		wService.getCodeUrl("http://web.tnb99.cn/index.html?type=consumer");
//		dService.getCustomerWithDeviceNo("17c7f722","000017c7");
//		Date date1=new Date(2017-1900, 05, 05);
//		int interval = (int) ((System.currentTimeMillis() - date1.getTime()) / (1000*3600*24));
//		testService.deleteStatData();
//		Calendar cal = Calendar.getInstance();
//		cal.set(Calendar.YEAR, 2017);
//		cal.set(Calendar.MONTH, 05);
//		cal.set(Calendar.DAY_OF_MONTH, 5);
//		for(int i=0;i<interval-1;i++){
//			cal.add(Calendar.DATE, 1);
//			Date date = cal.getTime();
//			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			String dateStr = sim.format(date);
//			System.out.println(dateStr);
//			testService.statInTryBackQuery(dateStr);
//			testService.statConsultQuery(dateStr);
//			testService.statOldManQuery(dateStr);
//		}
//		nursingService.getEmployeeByCID(129L);	
//		employeeService.findByWorkTimeSetting(1002l,1498638371L) ;
//      sService.createCustomerDayBill();
//nursingService.getCustomerByUID(19l);
//		nursingService.getProjectItemFromPlanDetail(129l, 1499307204);
//		CustomerServerChange change=new CustomerServerChange();
//		Customer customer=new Customer();
//		customer.setId(242l);
//		change.setId(199l);
//		change.setCustomer(customer);
//		cService.operateCustomerServerChange(change);
//		sService.createCustomerDayBill();
//		logger.debug("start test personlocation");
//		sService.sysPersonLocationReport();
//		sService.sysNightPersonLocationReport();
//		logger.debug("end test personlocation");
//		lService.addDeviceInfo("111");
		sService.statStaffAttendance();
//      sService.statStaffWorkTime();
//		testService.attendance();
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("test stat end".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug(" test stat error " + e);
		}
	}

	
	@RequestMapping("/back")
	@ResponseBody
	public void testCustomerBack(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("-----------------------------start test back-------------------------------------");
		testService.testCustomerBack();
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("test stat end".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug(" test stat error " + e);
		}
	}
	
	@RequestMapping("/yesback")
	@ResponseBody
	public void testYesCustomerBack(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("-----------------------------start yes test back-------------------------------------");
		testService.testYesCustomerBack();
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("test yes stat end".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug(" test stat error " + e);
		}
	}
	
	
	// 测试演示
	@RequestMapping("/startShow")
	@ResponseBody
	public void startShow(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		logger.debug("-----------------------------show   start-------------------------------------");
		String param = request.getParameter("flag");
		Long tenantId = (long) -1;
		if (param != null && !"".equals(param) && param.matches("\\d+")) {
			tenantId = Long.parseLong(param);// 获取所传tenantid
		}
		testService.startShowDeviceData(tenantId);
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("show end".getBytes());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.debug("show  error:"+e);
		}
		logger.debug("-----------------------------show   end-------------------------------------");
	}
	
	// 测试演示
	@RequestMapping("/endShow")
	@ResponseBody
	public void endShow(HttpServletRequest request, HttpServletResponse response) {
		// 数据处理
		logger.debug("-----------------------------show   end-------------------------------------");
		testService.endShowDeviceData();
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("show end".getBytes());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.debug("show  error:"+e);
		}
		logger.debug("-----------------------------show   end-------------------------------------");
	}
	
	
	@RequestMapping("/removeFlag")
	@ResponseBody
	public void removeFlag(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("-----------------------------start test back-------------------------------------");
		String param = request.getParameter("flag");
		if (param != null && !"".equals(param) && param.matches("\\d+")) {
			 ActionContextHolder.getPlanModelList().remove(param);
		}else{
	        ActionContextHolder.getPlanModelList().clear();
		}
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("removeFlag".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.debug(" test stat error " + e);
		}
	}
}
